package com.designPattern.strategy;


import com.designPattern.strategy.Vip.TicketPrice;

import java.io.File;
import java.io.FileFilter;
import java.lang.annotation.Annotation;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: Sunshine Insurance Group Co., Ltd.</p>
 *
 * @author lbb
 * @version 1.0
 * 修改记录：
 * 修改序号，修改日期，修改人，修改内容
 */
public class VipFactory {


    private static final String CAL_PRICE_PACKAGE = "com.designPattern.strategy.Vip";//这里是一个常量，表示我们扫描策略的包

    private ClassLoader classLoader = getClass().getClassLoader();

    private List<Class<? extends TicketPrice>> vipList;//策略列表


    public VipFactory() {
        this.init();
    }


    public TicketPrice creatTicketPrice(int viptype){


        for (int i = 0; i <vipList.size() ; i++) {

            PriceRegion priceRegion = getPriceRegion(vipList.get(i));
            int vip = priceRegion.Vip();
            if(viptype==vip){
                try {
                    return  vipList.get(i).newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
         return null;
    }






    private PriceRegion getPriceRegion(Class aClass){
        Annotation[] declaredAnnotations = aClass.getDeclaredAnnotations();
        if (declaredAnnotations == null || declaredAnnotations.length == 0) {
            return null;
        }
        for (int i = 0; i <declaredAnnotations.length ; i++) {
            if(declaredAnnotations[i] instanceof PriceRegion){
                  return (PriceRegion) declaredAnnotations[i];
            }
        }
       return  null;
    }


    private void init() {
        vipList = new ArrayList<>();
        File[] resources = getResources();
        String name = TicketPrice.class.getName();
        try {
            Class<TicketPrice> ticketPrices = (Class<TicketPrice>) classLoader.loadClass(name);
            for (int i = 0; i < resources.length; i++) {
                Class<?> clazz = classLoader.loadClass(CAL_PRICE_PACKAGE + "." + resources[i].getName().replace(".class", ""));
                if (TicketPrice.class.isAssignableFrom(clazz) && clazz != ticketPrices) {
                    vipList.add((Class<? extends TicketPrice>) clazz);
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    //获取扫描的包下面所有的class文件
    private File[] getResources() {
        try {
            File file = new File(classLoader.getResource(CAL_PRICE_PACKAGE.replace(".", "/")).toURI());
            return file.listFiles(new FileFilter() {
                public boolean accept(File pathname) {
                    if (pathname.getName().endsWith(".class")) {//我们只扫描class文件
                        return true;
                    }
                    return false;
                }
            });
        } catch (URISyntaxException e) {
            throw new RuntimeException("未找到策略资源");
        }
    }
}
